//
//  ViewController.swift
//  Animation_Ges
//
//  Created by lantaiyuan on 2017/8/28.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    
    @IBOutlet weak var pengImage: UIImageView!
    var frames : NSArray?
    var directer : CGPoint?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let image1 = UIImage(named :"penguin_walk01")!
        let image2 = UIImage(named :"penguin_walk02")!
        let image3 = UIImage(named :"penguin_walk03")!
        let image4 = UIImage(named :"penguin_walk04")!
        
        let frames : [UIImage] = [image1,image2,image3,image4]
        pengImage.animationDuration = 0.15
        pengImage.animationRepeatCount = 2
        pengImage.animationImages = frames
        
        //walk right
        let swipeGestureRight = UISwipeGestureRecognizer.init(target: self, action: #selector(walkRight(_:)))
        swipeGestureRight.direction = .right
        view.addGestureRecognizer(swipeGestureRight)
        
        
        //walk left
        let swipeGestureLeft = UISwipeGestureRecognizer.init(target: self, action: #selector(walkLeft(_:)))
        swipeGestureLeft.direction = .left
        view.addGestureRecognizer(swipeGestureLeft)
        
        
        //jump
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(jump(_:)))
        view.addGestureRecognizer(tap)
        
        
        //longpress
        let longPress = UILongPressGestureRecognizer.init(target: self, action: #selector(longPress(_:)))
        view.addGestureRecognizer(longPress)
    }
    func longPress(_ sender:UIGestureRecognizer) {
      UIView.animate(withDuration: 0.33, animations: {
        
        self.directer = self.pengImage.center
        self.pengImage.center = CGPoint.init(x: self.pengImage.center.x, y: self.view.frame.size.height)
      }) { (finished) in
        self.longPressBack()
        }
    }
    func longPressBack() {
        UIView.animate(withDuration: 0.25) { 
            self.pengImage.center = self.directer!
        }
    }
    func jump(_ sender:UIGestureRecognizer) {
        pengImage.startAnimating()
        UIView.animate(withDuration: 0.25, animations: { 
            self.pengImage.center = CGPoint.init(x: self.pengImage.center.x, y: self.pengImage.center.y - 50)
        }) { (finished) in
            self.jumpBack()
        }
    }
    func jumpBack() {
        UIView.animate(withDuration: 0.25) { 
            self.pengImage.center = CGPoint.init(x: self.pengImage.center.x, y: self.pengImage.center.y + 50)
        }
    }
    func walkRight(_ sender :UIGestureRecognizer) {
        if pengImage.center.x > view.frame.size.width {
            self.pengImage.center = CGPoint.init(x: 0, y: self.pengImage.center.y)
        }
        self.pengImage.transform = CGAffineTransform.identity
        pengImage.startAnimating()
        UIView.animate(withDuration: 0.5) { 
            self.pengImage.center = CGPoint.init(x: self.pengImage.center.x + 30, y: self.pengImage.center.y)
        }
    }
    func walkLeft(_ sender:UIGestureRecognizer) {
        if pengImage.center.x < 0.0 {
            pengImage.center = CGPoint(x:view.frame.size.width,y:pengImage.center.y)
        }
        
        self.pengImage.transform = CGAffineTransform.init(scaleX: -1.0, y: 1.0)
        pengImage.startAnimating()
        UIView.animate(withDuration: 0.5) { 
            self.pengImage.center = CGPoint.init(x: self.pengImage.center.x - 30, y: self.pengImage.center.y)
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

